#!/bin/sh
print_error () {
  local ERROR_MESSAGE="$1"
  local ERROR_CODE="$2"

  echo "$ERROR_MESSAGE" 1>&2

   if [ $ERROR_CODE ]
   then
      EXIT_CODE=$ERROR_CODE
   else
      EXIT_CODE=1
   fi
}

ERROR_WRAPPER_OPENED=false
print_error_wrapper () {
   if $ERROR_WRAPPER_OPENED;
   then
      ERROR_WRAPPER_OPENED=false
      print_error "\r\n"
      print_error "-----------------------------------------------------------------------------"
      print_error "\r\n"
   else
      print_error "\r\n"
      print_error "------------------------------------------------------------------------------"
      print_error "                                 ERROR OCCURRED                               "
      print_error "\r\n"
      ERROR_WRAPPER_OPENED=true
   fi
}

svn_cat_tmp_file () {
   local TXN="$1"
   local REPOS="$2"
   local FILE="$3"
   TMP_DIR=`mktemp -d`
   TMP_FILENAME=`basename "$FILE"`
   TMP_FILE="$TMP_DIR/$TMP_FILENAME"

   `$SVNLOOK cat -$SVN_REVISION_ATTR "$TXN" "$REPOS" "$FILE" > $TMP_FILE`

   echo $TMP_FILE
}

trim () {
   echo "$1" | sed 's/^ *//g' | sed 's/ *$//g'
}

EXIT_CODE="0"

REPOS="$1"
TXN="$2"
PHP="php5"
SVNLOOK="svnlook"

ADD_SVN_CHECK_SVN_LOG=true
ADD_SVN_CHECK_PHP_SYNTAX=true
ADD_SVN_CHECK_PHPDOC=true
ADD_SVN_CHECK_COMPAT_IE=true


if [ -e "$REPOS/hooks/add-svn-configure" ]
then
   $REPOS/hooks/add-svn-configure
fi


if [ $3 ]
then
   SVN_REVISION_ATTR=$3
else
   SVN_REVISION_ATTR="t"
fi


UPDATED_FILES=`$SVNLOOK changed -$SVN_REVISION_ATTR "$TXN" "$REPOS" | grep -E '^(U|A)' | awk '{print $2}'`

# This means no files are updated
if [ "$UPDATED_FILES" = "" ]
then
   exit 0
fi

##
 # Checking the log message
 ##
if $ADD_SVN_CHECK_SVN_LOG;
then
   LOG_MESSAGE=`$SVNLOOK log -$SVN_REVISION_ATTR "$TXN" "$REPOS"`

   if [ "$LOG_MESSAGE" = "" ]
   then
      print_error_wrapper
      print_error "Please input your log message"
      print_error_wrapper
      EXIT_CODE=2
   fi
fi

PHP_FILES=`echo "$UPDATED_FILES" | grep .php$`

for FILE in $PHP_FILES
  do

   svn_cat_tmp_file "$TXN" "$REPOS" "$FILE"

   ##
    # PHP Syntax Error Check
    #
    # Partial Idea From
    # http://pookey.co.uk/wordpress/archives/34-automatically-syntax-checking-of-php-files-when-checking-$SVN_REVISION_ATTRo-svn
    # http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/syntax-check.sh
    ##
   if $ADD_SVN_CHECK_PHP_SYNTAX;
   then
      SYNTAX_ERROR=`$PHP -l -f $TMP_FILE 2>&1`
      SYNTAX_CHECK=$?
      if [ $SYNTAX_CHECK -ne 0 ]
      then
         print_error_wrapper
         print_error "Syntax Error#$SYNTAX_CHECK: $FILE"
         print_error "\r\n\r\n'$SYNTAX_ERROR'\r\n\r\n"
         if [ "$SYNTAX_OUTPUT" != "" ]
         then
            print_error "'$SYNTAX_OUTPUT'"
         fi
         print_error_wrapper
         EXIT_CODE=3
      fi
   fi

   # PHPDOC Checking
   if $ADD_SVN_CHECK_PHPDOC ;
   then
      PHPDOC_ERROR=`phpdoc --force -f "$TMP_FILE" -t "$TMP_DIR/docs" | grep "$TMP_FILE" -A20 | grep "Storing structure" -B20 | grep "  " | grep -vE "No page\-level DocBlock was found in file .*\.class\.php"`

      if [ "$PHPDOC_ERROR" != "" ]
      then
         print_error_wrapper
         print_error "Documentation Error For: $FILE"
         print_error "$PHPDOC_ERROR"
         print_error_wrapper
         EXIT_CODE=4
      fi
   fi
done

HTML_FILES=`echo "$UPDATED_FILES" | grep -E ".(php|html|tpl)$"`

for FILE in $HTML_FILES
   do
   TMP_FILE=`svn_cat_tmp_file "$TXN" "$REPOS" "$FILE"`
   ##
    #
    # button name value does not work on IE
    #
    ##
   if $ADD_SVN_CHECK_COMPAT_IE;
   then
      BUTTON_NAME_VALUE=`grep -E '\<button\s+.*?(name\=.*?value\=|value\=.*?name\=).*?\>' $TMP_FILE`

      if [ "$BUTTON_NAME_VALUE" != "" ]
      then
         print_error_wrapper
         print_error "Standards Violation: button and name and value (does not work on IE8 below)"
         print_error "`trim \"$BUTTON_NAME_VALUE\"`"
         print_error_wrapper
         EXIT_CODE=5
      fi
   fi

done

exit $EXIT_CODE